<template>
	<div>
		<AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { ExtendFormConfig } from '../../config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { setTempParams,setLoading } = useCommonStore()
const { getValue ,getContent} = useOperate(4)
const nowRowData: any = inject('nowRowData')

const emit = defineEmits(['submit'])
const formModel = ref({
	dpServiceOrgHonorList: [],
})
const formRef = ref(null)
let baseData:any= markRaw({})

// 数据变更处理

const onSubmit = (form, done) => {
	const params = {
		...form,
	}

	// 处理变更字段
	let changeColumnsBase = ExtendFormConfig.column.filter((item) => item.changeField)

	let changeData = []
	changeColumnsBase.map((item) => {
		const tempBefore = baseData[item.prop]
		const tempAfter = params[item.prop]
		if (tempBefore !== tempAfter) {
			changeData.push({
				label: item.label,
				before: getValue(tempBefore, item),
				after: getValue(tempAfter, item),
			})
		}
	})

	const emptyObj = {
    creditCode: params.creditCode,
		code: undefined,
		areas_name: undefined,
		areas: undefined,
    region:undefined,
    region_name:undefined,
    dpServiceOrgHonorList:undefined,
	}
	const changeObj = {
		data: changeData,
		dataChangeContentList: [
       ...getContent(baseData.dpServiceOrgHonorList, params.dpServiceOrgHonorList, 3036, '养老服务运营单位-荣誉或奖励', emptyObj, 'id'),
			{
				changeBefore: {
					...baseData,
					...emptyObj,
				},
				changeAfter: {
					...params,
					...emptyObj,
				},
				changeData: {
					...params,
					...emptyObj,
				},
				changeType: '养老服务运营单位',
				code: 3035,
			},
		],
	}
	setTempParams({
		name: params.orgHomeName,
		creditCode: params.creditCode,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
	done()
}

const onSave = () => {
	formRef.value!.onSubmit()
}
// 数据回显
const getInitData = () => {
  setLoading()
	apiMod.dpServiceOrgHome_detail(nowRowData.id).then((res) => {
		const data = {
			...res.data,
		}
		formModel.value = data
		baseData = Object.freeze(JSON.parse(JSON.stringify(data)))
    setLoading()
	})
}

onMounted(() => {
	getInitData()
})

defineExpose({
	onSave,
})
</script>

<style scoped></style>
